﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
#if WINDOWS_PHONE
using Microsoft.Xna.Framework.Input.Touch;
#endif
using Microsoft.Xna.Framework.Media;
using Neat;
using Neat.MenuSystem;
using Neat.EasyMenus;
using System.IO;

namespace RiverRaidX
{
    public class OptionsMenuX : Menu
    {
        public OptionsMenuX(NeatGame G)
            : base(G)
        {
        }


        bool fullscreen2, mute2, music2;
        void OpenOptionsMenu()
        {
            fullscreen2 = game.FullScreen;
            mute2 = game.MuteAllSounds;
            music2 = game.MuteMediaPlayer;

            System.Items[0].Caption = ("Fullscreen: " + (fullscreen2 ? "ON" : "OFF"));
            System.Items[1].Caption = ("Sounds: " + (mute2 ? "OFF" : "ON"));
            System.Items[2].Caption = ("Music: " + (music2 ? "OFF" : "ON"));
            try
            {
                System.Items[3].Caption = ("Effects: " + (game.Console.Ram["rr_effects"].ReturnBool() ? "ON" : "OFF"));
            }
            catch (Exception e)
            {
                game.Console.WriteLine(e.Message);
            }
        }

        public override void Activate()
        {
            OpenOptionsMenu();
            base.Activate();
        }

        void FullscreenChanged(MenuItem item)
        {
            fullscreen2 = !fullscreen2;
            item.Caption = "Fullscreen: " + (fullscreen2 ? "ON  " : "OFF  ");
        }

        void SoundsChanged(MenuItem item)
        {
            //game.MuteAllSounds = !game.MuteAllSounds;
            mute2 = !mute2;
            item.Caption = ("Sounds: " + (mute2 ? "OFF" : "ON"));
        }

        void MusicChanged(MenuItem item)
        {
            music2 = !music2;
            item.Caption = "Music: " + (music2 ? "OFF" : "ON");
        }

        void EffectsChanged(MenuItem item)
        {
            bool newVal = !game.Console.Ram["rr_effects"].ReturnBool();
            //game.Console.Run("rr_postfx " + (newVal).ToString());
            game.Console.Run("dv rr_effects " + (newVal).ToString());
            item.Caption = "Effects: " + (game.Console.Ram["rr_effects"].ReturnBool() ? "ON" : "OFF");
        }

        void SaveSettings(MenuItem item)
        {
            StreamWriter sw = new StreamWriter("options.nsc");
            //sw.WriteLine("dv e_music " + game.Console.Ram["e_music"].ReturnString());
            sw.WriteLine("g_fullscreen " + fullscreen2.ToString());
            sw.WriteLine("a_mutesounds " + mute2.ToString());
            sw.WriteLine("a_mutesongs " + music2.ToString());
            sw.WriteLine("dv rr_effects " + (game.Console.Ram["rr_effects"].ReturnBool()).ToString());
            sw.WriteLine("g_reinit");
            sw.Close();
            game.FullScreen = fullscreen2;
            game.MuteAllSounds = mute2;
            game.MuteMediaPlayer = music2;
            game.InitializeGraphics();
            Reset();
        }

        public override void CreateMenu()
        {
            fullscreen2 = game.FullScreen;
            System.AddItem("Fullscreen: " + (fullscreen2 ? "ON" : "OFF"));
            System.GetLastMenuItem().OnSelect = FullscreenChanged;
            System.AddItem("Sounds: " + (mute2 ? "OFF" : "ON"));
            System.GetLastMenuItem().OnSelect = SoundsChanged;
            System.AddItem("Music: " + (music2 ? "OFF" : "ON"));
            System.GetLastMenuItem().OnSelect = MusicChanged;
            System.AddItem("Effects: ");// + (game.Console.Ram["rr_effects"].ReturnBool() ? "ON" : "OFF"));
            System.GetLastMenuItem().OnSelect = EffectsChanged;
            System.AddItem("Apply Settings", true);
            System.GetLastMenuItem().OnSelect = SaveSettings;
            System.AddItem("Back", true, null, "sh mainmenu");
            System.ItemsOffset = new Vector2(0, 100);
            Activate();
            base.CreateMenu();
        }

        public override void HandleInput(GameTime gameTime)
        {
            if (game.IsTapped(Keys.Escape) || game.IsTapped(Buttons.Back))
                game.Console.Run("sh mainmenu");

            base.HandleInput(gameTime);
        }

        public override void Render(GameTime gameTime)
        {
            game.SpriteBatch.Draw(game.GetTexture("menuBackground"), new Rectangle(0, 0, game.GameWidth, game.GameHeight), Color.White);
            game.SpriteBatch.Draw(game.GetTexture("transparent"), new Rectangle(0, 0, game.GameWidth, game.GameHeight), Color.Black);

            base.Render(gameTime);
        }
    }
}
